package com.gree.changying.activity.mainactivity.framgent;


import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.gree.changying.R;
import com.gree.changying.activity.base.BasePageFragment;
import com.gree.changying.activity.mainactivity.adapter.FrmPagerAdapter;
import com.gree.changying.activity.mainactivity.framgent.homefrm.BifenFrm;
import com.gree.changying.activity.mainactivity.framgent.homefrm.TuijianFrm;
import com.gree.changying.activity.mainactivity.framgent.homefrm.XinwenFrm;
import com.gree.changying.activity.mainactivity.framgent.homefrm.ZhishuFrm;
import com.gree.changying.activity.mainactivity.framgent.homefrm.ZuQiuFenxiFrm;
import com.gree.changying.widget.tablayout.jtablayout.tab.Tab;
import com.gree.changying.widget.tablayout.widget.JTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class HomeFrm extends BasePageFragment {
    @Override
    protected void initView(View view) {
        ButterKnife.bind(this,view);
        initPage();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.frm_home;
    }

    private String[] tabMenu = {
            "推荐","足球分析","新闻","比分","指数"
    };
    private FrmPagerAdapter mAdapter;
    private List<Fragment> mFragments;
    @Bind(R.id.mTabLayout)
    JTabLayout mTabLayout;
    @Bind(R.id.mViewPager)
    ViewPager mViewPager;
    private TuijianFrm tuijianFrm;
    private ZuQiuFenxiFrm zuQiuFenxiFrm;
    private XinwenFrm xinwenFrm;
    private BifenFrm bifenFrm;
    private ZhishuFrm zhishuFrm;
    private void initPage(){
        mFragments = new ArrayList<>();
        tuijianFrm = new TuijianFrm();
        zuQiuFenxiFrm = new ZuQiuFenxiFrm();
        xinwenFrm = new XinwenFrm();
        bifenFrm = new BifenFrm();
        zhishuFrm = new ZhishuFrm();
        mFragments.add(tuijianFrm);
        mFragments.add(zuQiuFenxiFrm);
        mFragments.add(xinwenFrm);
        mFragments.add(bifenFrm);
        mFragments.add(zhishuFrm);
        FragmentManager fragmentManager  = getChildFragmentManager();
        mAdapter = new FrmPagerAdapter(fragmentManager,mFragments);

        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(5);
        mViewPager.setCurrentItem(0);
        for(int i = 0 ;i<tabMenu.length;i++){
            Tab tab = mTabLayout.newTab().setTitle(tabMenu[i]);
            mTabLayout.addTab(tab);
        }
        initListener();
    }
    private void initListener(){
        mTabLayout.addOnTabSelectedListener(new JTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(@NonNull Tab var1) {
                int position = var1.getPosition();
                if(position!=mViewPager.getCurrentItem()) {
                    mViewPager.setCurrentItem(position);
                }
            }

            @Override
            public void onTabUnselected(@NonNull Tab var1) {

            }

            @Override
            public void onTabReselected(@NonNull Tab var1) {

            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTabLayout.selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

}
